/**
 * 通知偏好设置服务层
 */

// 默认设置
const defaultSettings = {
  methods: {
    sms: true,
    push: true,
    email: false,
    call: false
  },
  timeRange: {
    start: '08:00',
    end: '22:00'
  },
  types: {
    emergency: true,
    warning: true,
    info: true,
    system: true
  },
  doNotDisturb: {
    enabled: false,
    startTime: '22:00',
    endTime: '08:00',
    emergencyOverride: true
  },
  sound: {
    tone: 'default',
    vibrate: true
  },
  frequency: {
    interval: 15
  }
};

// 模拟用户通知设置数据
const mockNotificationSettings = {
  methods: {
    sms: true,
    push: true,
    email: true,
    call: false
  },
  timeRange: {
    start: '07:00',
    end: '23:00'
  },
  types: {
    emergency: true,
    warning: true,
    info: false,
    system: true
  },
  doNotDisturb: {
    enabled: true,
    startTime: '22:30',
    endTime: '07:30',
    emergencyOverride: true
  },
  sound: {
    tone: 'crisp',
    vibrate: true
  },
  frequency: {
    interval: 10
  }
};

/**
 * 获取通知设置
 */
export function getNotificationSettings() {
  return new Promise((resolve) => {
    // 模拟网络延迟
    setTimeout(() => {
      // 从本地存储获取或使用模拟数据
      const savedData = localStorage.getItem('notificationSettings');
      if (savedData) {
        resolve(JSON.parse(savedData));
      } else {
        resolve({ ...mockNotificationSettings });
      }
    }, 500);
  });
}

/**
 * 更新通知设置
 */
export function updateNotificationSettings(settings) {
  return new Promise((resolve, reject) => {
    // 模拟网络延迟
    setTimeout(() => {
      try {
        // 验证数据
        if (!settings.methods || !settings.timeRange || !settings.types) {
          reject(new Error('设置数据不完整'));
          return;
        }

        // 保存到本地存储
        localStorage.setItem('notificationSettings', JSON.stringify(settings));
        resolve({ success: true });
      } catch (error) {
        reject(new Error('保存失败'));
      }
    }, 800);
  });
}

/**
 * 获取默认设置
 */
export function getDefaultSettings() {
  return JSON.parse(JSON.stringify(defaultSettings));
}

/**
 * 测试通知（模拟发送测试通知）
 */
export function sendTestNotification(method) {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        success: true,
        message: `测试通知已通过${method}发送`
      });
    }, 1000);
  });
}

/**
 * 获取通知历史统计
 */
export function getNotificationStatistics() {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        total: 245,
        byMethod: {
          sms: 89,
          push: 132,
          email: 24,
          call: 0
        },
        byType: {
          emergency: 12,
          warning: 45,
          info: 156,
          system: 32
        },
        lastWeek: [
          { date: '2025-10-29', count: 32 },
          { date: '2025-10-30', count: 28 },
          { date: '2025-10-31', count: 35 },
          { date: '2025-11-01', count: 41 },
          { date: '2025-11-02', count: 38 },
          { date: '2025-11-03', count: 44 },
          { date: '2025-11-04', count: 27 }
        ]
      });
    }, 600);
  });
}

